package com.sso.cas;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by 青木 on 2015/9/10.
 */
@Repository
public class UserDaoJdbc {
    private static final String SQL_VERIFY_ACCOUNT = "SELECT count(*) FROM _USER WHERE username=? AND password=?";
    private static final String SQL_USER_GET = "SELECT * FROM _USER WHERE username=?";
    private JdbcTemplate jdbcTemplate;

    @Resource
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }


    public boolean verifyAccount(String username, String password) {
        try {
            return 1 == this.jdbcTemplate.queryForObject(SQL_VERIFY_ACCOUNT, new Object[]{username, password}, Integer.class);
        } catch (EmptyResultDataAccessException e) {
            return false;
        }
    }

    public User getByUsername(String username) {
        try {
            return (User) this.jdbcTemplate.queryForObject(SQL_USER_GET, new Object[]{username}, new UserRowMapper());
        } catch (EmptyResultDataAccessException e) {
            return new User();
        }
    }
}


class UserRowMapper implements RowMapper<User> {
    @Override
    public User mapRow(ResultSet rs, int index) throws SQLException {
        User user = new User();
        user.setId(rs.getInt("id"));
        user.setUsername(rs.getString("username"));
        user.setPassword(rs.getString("password"));
        return user;
    }
}